<template>
  <div class="login_page">
    <div class="title">欢迎来到图片管理中心</div>
    <div v-if="scanLoginRef">
      <div class="scan_login">
        <div class="scan_login_img"></div>
        <div>请扫描二维码登录</div>
        <el-button type="primary" @click="scanLoginRef = false">账号密码登录</el-button>
      </div>
    </div>
    <div class="form" v-else>
      <el-form :model="form" label-width="auto">
        <el-form-item label="用户账号">
          <el-input v-model="form.count" />
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item class="form_footer">
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button type="primary" @click="onRegister">注册</el-button>
          <el-button type="primary" @click="scanLoginRef = true">扫描登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="test-watch"></div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useGlobalStore } from "@/store";
const globalStore = useGlobalStore();
const router = useRouter(); // 路由实例
const scanLoginRef = ref(true); // 是否扫描登录

const form = reactive({
  count: "", // 用户账号
  password: "", // 用户密码
});

/**
 * 登录
 */
const onSubmit = () => {
  console.log("form", form);
  if (form.count === "admin" && form.password === "123456") {
    router.replace("/index");
  }
};

/**
 * 注册
 */
const onRegister = async () => {
   const flag = await globalStore.registerUser(form.count, form.password);
    flag === true && router.replace("/index");
 
};
</script>

<style lang="css" scoped>
.login_page {
  margin: auto;
  height: 100%;
}
.title {
  font-size: 32px;
  text-align: center;
  margin-block: 32px;
}
.form,
.scan_login {
  width: 500px;
  height: 500px;
  border: 1px solid #ccc;
  margin: auto;
}
.form {
  padding-top: 100px;

  padding-inline: 40px;
}
.form_footer {
  margin-top: 60px;
}
.scan_login {
  text-align: center;
}
.scan_login_img {
  width: 200px;
  height: 200px;
  margin: auto;
  margin-top: 100px;
  background-color: #25b68d;
  margin-bottom: 40px;
}
</style>
